package com.jj.fly.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.jj.fly.utils.LogUtils;

/**
 * 全局异常处理
 * 
 * @author JJ
 *
 */
public class CustomExceptionResolver implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {

		CustomException customException = null;
		Log logger = LogUtils.getFrameworkLoger();
		logger.error(ex);
		if (ex instanceof CustomException) {
			customException = (CustomException) ex;
		} else {
			customException = new CustomException("未知错误");
		}
		// 错误信息
		String message = customException.getMessage();

		ModelAndView mv = new ModelAndView();
		mv.addObject("message", message);
		mv.setViewName("/error/error-500");
		return mv;
	}
}
